<?php
class system_setting_concept extends system_concept_abstract {
	
	public function __construct($lang) {
		parent::__construct(array('lang' => $lang), array('lang = ?' => $lang));
	}
	
	public function init() {
		$this->setRowClass('system_setting_row');
		$this->setModel('system_setting_model', 'setting');
		$this->setModelCols('setting', array('lang', 'name', 'value', 'description', 'is_system'));
	}
	
	public function save(system_setting_row $row, $isNew = true) {
		if(!preg_match('/^[a-zA-Z]{1}[\w]*$/is', $row->getName())) {
			throw new system_exception("Setting Name formation error.");
		}
		if($isNew) {
			if($this->fetch(array('name = ?' => $row->getName()))) {
				throw new system_exception("This setting is already Existing.");
			}
			$this->getModel('setting')->insert($row->getModelData('setting'));
		} else {
			if(!$this->fetch(array('name = ?' => $row->getName()))) {
				throw new system_exception("This setting is not Existing.");
			}
			$row->update();
		}
		return $row;
	}
	
	public function delete(system_setting_row $row) {
		if($row->getIsSystem()) {
			throw new system_exception("It can not be Deleted.");
		}
		return parent::delete($row);
	}

}